<template>
  <div>
    <el-form ref="form" :model="form" :rules="formRules" label-width="100px">
      <el-form-item prop="name" label="角色">
        <el-input v-model="form.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item prop="permissions" label="权限">
        <PermissionSelect v-model="form.permissions" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { create } from '../../../api/role'
export default {
  components: {
    PermissionSelect: () => import('./components/PermissionSelect')
  },
  data() {
    return {
      formRules: {},
      form: {
        permissions: [],
        name: ''
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          create(this.form).then(() => {
            this.$message.success('保存成功')
            this.$router.push({
              name: 'role-index'
            })
          })
        } else {
          console.log('fail')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
